package org.dromara.run.controller;

import java.text.ParseException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.run.domain.bo.LevelBo;
import org.dromara.run.domain.vo.LevelVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.RecordVo;
import org.dromara.run.domain.bo.RecordBo;
import org.dromara.run.service.IRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 阳光跑跑步记录表
 *
 * @author chl
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/record")
public class RecordController extends BaseController {

    private final IRecordService recordService;

    /**
     * 查询阳光跑跑步记录表列表
     */
    @SaCheckPermission("run:record:list")
    @GetMapping("/list")
    public TableDataInfo<RecordVo> list(RecordBo bo, PageQuery pageQuery) {
        return recordService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询阳光跑等级列表详细信息
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:record:query")
    @GetMapping("/infoList")
    public R<List<RecordVo>> getInfoList(RecordBo bo) {
        return R.ok(recordService.queryList(bo));
    }
    /**
     * 导出阳光跑跑步记录表列表
     */
    @SaCheckPermission("run:record:export")
    @Log(title = "阳光跑跑步记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RecordBo bo, HttpServletResponse response) {
        List<RecordVo> list = recordService.queryList(bo);
        AtomicInteger index = new AtomicInteger(1);
        list.forEach(vo-> {
            // 序号递增
            vo.setIndex(index.getAndIncrement());
        });
        ExcelUtil.exportExcel(list, "阳光跑跑步记录表", RecordVo.class, response);
    }

    /**
     * 获取阳光跑跑步记录表详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("run:record:query")
    @GetMapping("/{recordId}")
    public R<RecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long recordId) {
        return R.ok(recordService.queryById(recordId));
    }

    /**
     * 新增阳光跑跑步记录表
     */
    @SaCheckPermission("run:record:add")
    @Log(title = "阳光跑跑步记录表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RecordBo bo) throws ParseException {
        return toAjax(recordService.insertByBo(bo));
    }

    /**
     * 修改阳光跑跑步记录表
     */
    @SaCheckPermission("run:record:edit")
    @Log(title = "阳光跑跑步记录表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RecordBo bo) {
        return toAjax(recordService.updateByBo(bo));
    }

    /**
     * 删除阳光跑跑步记录表
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("run:record:remove")
    @Log(title = "阳光跑跑步记录表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] recordIds) {
        return toAjax(recordService.deleteWithValidByIds(List.of(recordIds), true));
    }
}
